import { setStorage, getStorage } from './storage';


const timestampToTime = () => {
  var date = new Date();//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}
// 设置分数
export const setScore = (level, scoreNum) => {
  let list = getStorage(`level-${level}-score`) || [];
  // @ts-ignore
  list.push({
    score: scoreNum,
    time: timestampToTime()
  })
  // @ts-ignore
  list.sort((a, b) => {
    return b.score - a.score;
  })
  // @ts-ignore
  setStorage(`level-${level}-score`, list.slice(0, 5))
}

// 获取分数
export const getScore = (level) => {
  return getStorage(`level-${level}-score`)
}

